﻿using HslCommunication.Profinet.Siemens;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Testudo.WebApp.Events;
using Testudo.WebApp.Hubs;

namespace Testudo.WebApp.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class WcsNotifyController : ControllerBase
{
    public WcsNotifyController(IHubContext<WcsProxyHub> hubContext)
    {
        HubContext = hubContext;
    }

    private IHubContext<WcsProxyHub> HubContext { get; }

    [HttpPost]
    public async Task<ActionResult> NotifyMessage(string message)
    {
        await HubContext.Clients.All.SendAsync("Notify", new NotifyMessage { Message = message });
        ;
        return Ok();
    }

    [HttpPost]
    public async Task<IActionResult> ReadFromPlc()
    {
        var plc = new SiemensS7Net(SiemensPLCS.S1500);
        plc.IpAddress = "192.168.1.208";
        plc.Port = 102;
        plc.ConnectTimeOut = 10000; // 连接超时，单位毫秒
        plc.ReceiveTimeOut = 5000; // 接收超时，单位毫秒
        plc.Rack = 0;
        plc.Slot = 0;


        var result = await plc.ReadInt16Async("DB1.860");
        var v = result.IsSuccess ? result.Content : 0;

        return Ok(v);
    }
}